Input Devices

This week assignment is to measure something by adding a sensor to a microcontoller board that we have designed.

I will be using IR sensor to measure distance of the object and displaying it on serial commmunication window.

First step is to read the data sheet of the sensor and to know about its characterstic.

From the block diagram of the IR sensor shoen below it can be concluded that basically it has 2-wires for supply voltage (4.5-5V),2 wires for ground and 1-wire for input.

Input-Output Characterstic.

Features of SHARP_GP2Y0A710YK0F

1.Long distance type Distance measuring range : 100 to 550 cm

2. Analog output type

3. Package size : 58x17.6x22.5 mm

4. Consumption current : Typ. 30 mA

5. Supply voltage : 4.5 to 5V

Collecting Data using Arduino UNO.

To understand the basics of how to read analog data and how ADC works in Arduino UNO i have gone through this basic tutorail.

How a ADC works in microcontroller?

Electronics Design

For this i will be using ATtiny45 as microcontroller for input devices. This board is designed in eagle for which files are shared below.


inputdevices.sch inputdevices.brd traces.png interior.png

Follow this link to see the process involved in designing the PCB.

Stuffed Board


Check the Board

Serial Communicaton in ATtiny44a

As discussed with my local instructor next step is to learn how serial communication works in Attiny44a. He suggested me source for this for which lnk is given below.

Now i will be testing serial communication codes on this board. In this program printing will be displayed when a letter c is passed through serial communication window, other wise it will print No print


#include<SoftwareSerial.h>

 const byte rxPin = 3;
 const byte txPin= 4;
 SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin 13 as an output.
 

  
    // define pin modes for tx, rx:
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
}

// the loop function runs over and over again forever
void loop() {

//listen to serial port
if (mySerial.read()=='c')
{
  mySerial.println("print");
  delay(1000);
}
else
 {
 mySerial.println("No print");
  delay(1000);
 }
  }

Output

After burning the above program through FabISP into the board i used FTDI connector for serial communication with the board.

Adding IR Sensor

I followed the source code from this link which is written and tested by Michael from DFrobot.

I made the following changes in the above code.

1.This code was previously for Arduino, i made it to work with ATtiny 44.

2.Changed the analog input pin .

3.Changed the code to display distance in mm.

4.Removed the LED ON/OFF part.


/*Description: sharp 2Y0A710K distance ranger.(Range from 1m to 5.5m.)
This code prints out data of valid range.
However, if we put ranger very close to objects, we will have completely wrong results.
The error is inevitable because of electric character of this device. 
So the only way of correct use is to make sure the distance of objects  not close to the ranger. (1m to 5.5m.)
Another tip: it is not very precise. So it is fit for detection,but not for measurement.       

Tested by Michael from DFrobot 
2012/2/13*/

/*Principle of this ranger: (See details in datasheet.)
Voltage is linear to the inverse of distance. (Distance range is 1m to 5.5m)
Two reference points:
Analog Voltage(V)   Digital value(0-1023)    Distance(cm)
  2.5                512                     100
  1.4                286                     500
Then, we have a linear equation of digital value and distance.
(512-sensorValue)/(1/100-1/distance)=(512-286)/(0.01-0.002) 
=> distance=28250/(sensorValue-229.5);
*/
#include 
 const byte rxPin = 3;
 const byte txPin= 4;
 SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);
void setup() {
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  pinMode(rxPin, INPUT);
  pinMode(0, INPUT);
mySerial.begin(9600);

}
int sensorValue;
int distance;
void loop() {
  
 sensorValue = analogRead(0);   
//if(sensorValue>=280&&sensorValue<=512)  //Corresponding distance range from 1m to 5.5m
 //{
  // mySerial.print("The distance is: ");
  // distance=28250/(sensorValue-229.5);
  //mySerial.println(distance);
 // mySerial.println("cm");
 //}
 mySerial.println(sensorValue);
  delay(200);                  
}




Learning form the CODE.

From this code i learned how to read data from the analog pins and how to convert the data available from analog pin into some suitable form such as "distance" in this case.

Output

Problem faced

I tried the above program with above milled board, it was showing constant reading. So for the mean time i used my hello board for input devices.

The output of the IR sensor obtained is shown in figure below.



The value obtained from IR sensor must be converted into distance. From the characterstic of the IR sensor the formula obtained to convert voltage into distance is shown below.

Debugging

Now to debug the problem with milled board first i checked the connections of my board. Then i observed that the pin 0 which i used for input is actually used for Aref which provides constant 5 Volts. I changed the input pin to 1, now it start working.

I modified this code to show distance in mm as it is convinient for me to work with data in mm. Below you can find the link of the modified code.

inputdevices.ino